package com.njmt.cloud.msprovder.controller;

import com.njmt.cloud.msprovder.entity.OrderEntity;
import com.njmt.cloud.msprovder.model.OrderModel;
import com.njmt.cloud.msprovder.model.UserModel;
import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author <a href="mailto:xingxiaofeng@gtmap.cn">xingxiaofeng</a>
 * @date 2022-05-11
 * @desc DozerController
 */
@RestController
@CacheConfig(cacheNames = "orders")
public class DozerController {
    @Resource
    private Mapper mapper;
    @GetMapping("/getDozer")
    public OrderEntity getDozer(){
        OrderEntity orderEntity=null;

            Mapper build = DozerBeanMapperBuilder.create().withMappingFiles("http://localhost:8072/metaData").build();
            orderEntity = toMap(build);

        return orderEntity;
    }
    
    @GetMapping("/getDozer/{path}")

    public OrderEntity getDozerXml(@PathVariable("path")String path){
        OrderEntity orderEntity=null;
        if(path!=null) {
            //dozer/order-dozer.xml
            Mapper build=getMapper(path);
            orderEntity = toMap(build);
            
        }
        return orderEntity;
    }

    @GetMapping("/getDozerMapper")
    public OrderEntity getDozerMapper(){
        OrderEntity orderEntity=null;
          orderEntity=toMap(mapper);
          return orderEntity;
    }

    @GetMapping("/metaData")
    public void metaData(HttpServletResponse response) {
        String xml = "";

        response.setContentType("text/xml;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            xml=getXml();
            out.write(xml);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out!=null) {
                out.flush();
                out.close();
            }
        }

    }
    private OrderEntity toMap(Mapper _mapper){
        OrderModel orderModel =new OrderModel();
        UserModel userModel=new UserModel();
        userModel.setId("1").setName("张三");
        orderModel.setId("1").setName("zhangsan").setAddress("南京").setUserModel(userModel);
        System.out.println("orderModel:"+orderModel.toString());
        System.out.println("userModel:"+orderModel.getUserModel().toString());
        OrderEntity orderEntity= _mapper.map(orderModel, OrderEntity.class,"order" +
                "");
        System.out.println("orderEntity:"+orderEntity.toString());
        System.out.println("userEntity:"+orderEntity.getUserEntity().toString());
          return orderEntity;
    }
    @Cacheable(key = "''+#path")
    public Mapper getMapper(String path){

        System.out.println("打印getMapper");
        Mapper build = DozerBeanMapperBuilder.create().withMappingFiles("dozer/"+path).build();
         return build;
    }
    private String getXml(){
        String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<mappings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "          xmlns=\"http://dozermapper.github.io/schema/bean-mapping\"\n" +
                "          xsi:schemaLocation=\"http://dozermapper.github.io/schema/bean-mapping\n" +
                "                             http://dozermapper.github.io/schema/bean-mapping.xsd\">\n" +
                "    <!-- 描述两个类中属性的对应关系，对于两个类中同名的属性可以不映射 -->\n" +
                "\n" +
                "    <mapping map-id=\"order\">\n" +
                "        <class-a>com.njmt.cloud.msprovder.entity.OrderEntity</class-a>\n" +
                "        <class-b>com.njmt.cloud.msprovder.model.OrderModel</class-b>\n" +
                "        <field>\n" +
                "            <a>vid</a>\n" +
                "            <b>id</b>\n" +
                "        </field>\n" +
                "        <field>\n" +
                "            <a>vname</a>\n" +
                "            <b>name</b>\n" +
                "        </field>\n" +
                "        <field>\n" +
                "            <a>userEntity</a>\n" +
                "            <b>userModel</b>\n" +
                "        </field>\n" +
                "    </mapping>\n" +
                "</mappings>";
        return xml;
    }
    public InputStream getInputStream(String str) {
        if (str != null && !"".equals(str.trim())) {
            try {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
                return inputStream;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
